package kaixo;

import com.ibm.saguaro.system.Radio;
import com.ibm.saguaro.system.RadioTxDone;
import com.ibm.saguaro.system.Timer;
import com.ibm.saguaro.system.Util;

public class Comunicaciones {
	
	//informacion Ruta para el IDS
		static int idsId=1;
		static int[] nora = {1,2,3,4,5};
		static int[] nundik = {1,2,3,3,3};
	//history	
		public Paquete[] historySend;
		public Paquete[] historyRecived;
		private int sendKont=0;
		private int recivedKont=0;
		
	//moterunnes
		static Kaixo rcount;
		static Timer tsend;
		static int i=0;
		static byte[] xmit;
		static long xmitDelay;
	    static RadioTxDone onDone;
	
	public Comunicaciones(int newIdsId,int[] newNora, int[] newNundik){
		idsId = newIdsId;
		nora = newNora;
		nundik = newNundik;
		historySend = new Paquete[5];
		historyRecived = new Paquete[10];
	}
	
	public void introducirPaqueteEnviado(byte[] pdu){
		this.historySend[sendKont] = new Paquete(pdu);
		this.sendKont++;
		if (sendKont==5){
			sendKont=0;
		}
	}
	
	public void introducirPaqueteRecivido(byte[] pdu,int pak){
		this.historyRecived[recivedKont] = new Paquete(pdu);
		this.recivedKont++;
		if (recivedKont==10){
			recivedKont=0;
		}
	}
	
	public void enviarACK(int idNodo){
		xmit = new byte[12];
		xmit[0] = Radio.FCF_BEACON;
		xmit[1] = Radio.FCA_SRC_SADDR;
		Util.set16le(xmit, 3, 0x22);
		Util.set16le(xmit, 5, 0x1234);
				
		xmit[8] = (byte)idsId; 
		xmit[9] = (byte)idNodo;
		xmit[10] = (byte)nundik[idNodo-1];
		xmit[11] = (byte)0;
		Radio.transmit(Radio.TXMODE_POWER_MAX, xmit, 0, 12, onDone);
}

	public void enviar(int idNodo, byte info, Nodo node){
			xmit = new byte[13];
			xmit[0] = Radio.FCF_BEACON;
			xmit[1] = Radio.FCA_SRC_SADDR;
			Util.set16le(xmit, 3, 0x22);
			Util.set16le(xmit, 5, 0x1234);
					
			xmit[8] = (byte)idsId; 
			xmit[9] = (byte)idNodo;
			xmit[10] = (byte)nundik[idNodo-1];
			xmit[11] = (byte)5;
			xmit[12] = info;
			node.setEsperandoACK(true);
			node.setUltimoPdu(xmit);
			Radio.transmit(Radio.TXMODE_POWER_MAX, xmit, 0, 13, onDone);
	}
}
